


def get_input(file_path):
    with open(file_path) as f:
        f.readlines()

import copy



def sort_list(input_list):
    """
    """
    input_list = [(k, input_list[k]) for k in range(len(input_list))]

    for i in range(len(input_list)):
        for j in range(i + 1, len(input_list)):
            if input_list[i][1] > input_list[j][1]:
                temp = input_list[i]
                input_list[i] = input_list[j]
                input_list[j] = temp
    return [item[0] for item in input_list]
             
def c4():
    #data = get_input()

    data = [
            [5, 1, 3, 8, 9, 22],
            [1, 1, 1],
            [1, 2, 1],
            [2, 1, 2] 
           ]

    #data = [[1,1,1]]
    out_put = []
    for each_data in data:
        sort = sort_list(each_data)
        value = [0] * len(each_data)
        value[sort[0]] = 1
        for index in range(1, len(sort)):
            cur_index = sort[index]
            pre_index = cur_index - 1
            aft_index = cur_index + 1
            if pre_index >= 0 and aft_index < len(each_data):
                max_num = (lambda x, y: x if x[1] >= y[1] else y)((pre_index, each_data[pre_index]),(aft_index, each_data[aft_index]))
                min_num = (lambda x, y: x if x[1] <= y[1] else y)((pre_index, each_data[pre_index]),(aft_index, each_data[aft_index]))
                if max_num == min_num:
                    if each_data[cur_index] == max_num[1]:
                        value[cur_index] = 1
                    elif each_data[cur_index] > max_num[1]:
                        value[cur_index] = value[max_num[0]] + 1
                    else:
                        value[cur_index] = value[max_num[0]] - 1
                if each_data[cur_index] > max_num[1]:
                    value[cur_index] = value[max_num[0]] + 1
                elif each_data[cur_index] == max_num[1]:
                    value[cur_index] = value[min_num[1]] + 1
                if each_data[cur_index] <= min_num[1]:
                    value[cur_index] = 1
                if each_data[cur_index] > min_num[1] and each_data[cur_index] < max_num[1]:
                    value[cur_index] = value[min_num[0]] + 1
            if cur_index == 0:
                if each_data[0] <= each_data[1]:
                    value[0] = value[1]
                else:
                    value[0] = value[1] + 1
            if cur_index == len(each_data) - 1:
                if each_data[cur_index] <= each_data[pre_index]:
                    value[cur_index] = 1
                else:
                    value[cur_index] = value[pre_index] + 1
        out_put.append((sum(value), each_data, value))
    return out_put

def test_sort_list():
    l1 = [1, 2, 3, 5]
    l2 = [5, 3, 2, 1]
    l3 = [1, 1, 1, 1]
    l4 = [1, 5, 2, 3]
    print sort_list(l1)
    print sort_list(l2)
    print sort_list(l3)
    print sort_list(l4)


#test_sort_list()
print c4()
